import 'package:basic_library/basic_library.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

///
/// 底部弹窗，选中放大的内容
///
class QShowBigTitleSheet extends Dialog {
  final BuildContext buildContext;
  final int index;
  final List<ItemDataModel> pickerChildren;
  final Function(int idx, ItemDataModel item)? sureCallBack;

  const QShowBigTitleSheet({
    Key? key,
    required this.buildContext,
    required this.pickerChildren,
    this.index = 0,
    this.sureCallBack,
  }) : super(key: key);

  /// 取消弹窗
  static hide(BuildContext buildContext) {
    Navigator.pop(buildContext);
  }

  /// 展示弹窗
  void show() {
    showCupertinoModalPopup(
      context: buildContext,
      builder: (BuildContext buildContext) {
        return _BottomContent(
          index: index,
          pickerChildren: pickerChildren,
          sureCallBack: sureCallBack,
        );
      },
    );
  }
}

class _BottomContent extends StatefulWidget {
  final int? index;
  final List<ItemDataModel>? pickerChildren;
  final Function(int idx, ItemDataModel item)? sureCallBack;

  const _BottomContent({
    Key? key,
    this.index,
    this.pickerChildren,
    this.sureCallBack,
  }) : super(key: key);

  @override
  _BottomContentState createState() => _BottomContentState();
}

class _BottomContentState extends State<_BottomContent> {
  FixedExtentScrollController _controller =
      FixedExtentScrollController(initialItem: 0);
  var state = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    for (int i = 0; i < widget.pickerChildren!.length; i++) {
      if (i == widget.index!) {
        _controller = FixedExtentScrollController(initialItem: i);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 250,
      color: Colors.white,
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: const Text(
                    '取消',
                    style: TextStyle(fontSize: 17, color: Colors.blue),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    if (widget.sureCallBack == null) return;
                    int idx = 0;
                    late ItemDataModel selectItem;
                    for (int i = 0; i < widget.pickerChildren!.length; i++) {
                      var item = widget.pickerChildren![i];
                      if (item.state == state) {
                        idx = i;
                        selectItem = item;
                      }
                    }
                    Navigator.pop(context);
                    widget.sureCallBack!(idx, selectItem);
                  },
                  child: const Text(
                    '确定',
                    style: TextStyle(
                      fontSize: 17,
                      color: Colors.blue,
                    ),
                  ),
                )
              ],
            ),
          ),
          Expanded(
            child: CupertinoPicker(
              scrollController: _controller,
              useMagnifier: true,

              /// 使用放大镜
              magnification: 1.3,

              /// 当前选中item放大倍数
              itemExtent: 50,

              /// 行高
              backgroundColor: Colors.white,

              /// 选中器背景色
              onSelectedItemChanged: (value) {
                state = widget.pickerChildren![value].state;
              },
              children: widget.pickerChildren!.map((data) {
                return Center(
                  child: Text(
                    data.name,
                    style: const TextStyle(
                      fontSize: 17,
                      color: Colors.black,
                    ),
                  ),
                );
              }).toList(),
            ),
          )
        ],
      ),
    );
  }
}
